﻿using SouthKoreaCoffee.IOModel;
using System.Threading;
using System;
using SouthKoreaCoffee.RobotModel;
using Newtonsoft.Json;
using SouthKoreaCoffee.Model;
using System.Text;
using System.Linq;
using SouthKoreaCoffee.SocketClientsModel;

namespace SouthKoreaCoffee.SocketClientsModel
{
    public class CoffeeModels
    {
        private static CoffeeModels instance;
        private CoffeeModels() { }

        public static CoffeeModels Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CoffeeModels();
                }
                return instance;
            }
        }
        /// <summary>
        /// 压盖流程
        /// </summary>
        /// <param name="LGInit">第几个压盖</param>
        public void YGMsg(int LGInit)
        {
            try
            {
                while (!IOMsg.Instance.CFBol) { Thread.Sleep(100); }
                IOMsg.Instance.CFBol = false;
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Lid_start);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                while (WuLiaoCoffeeSel.Instance.LGQFK1_Bol) { Thread.Sleep(10); }
                while (!WuLiaoCoffeeSel.Instance.LGQFK1_Bol) { Thread.Sleep(10); }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Lid_end);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                IOMsg.Instance.CFBol = true;
                RobotModelLeft.Instance.RobotQYG_UP();
                while (true)
                {
                    bool RobotBol = false;
                    Thread.Sleep(200);
                    RobotBol = RobotModelLeft.Instance.LRobotDYGD_Sel();
                    Thread.Sleep(200);
                    if (RobotBol)
                    {
                        RobotModelLeft.Instance.RobotQYG_Down();
                        break;
                    }
                }
                while (!IOMsg.Instance.CFBol) { Thread.Sleep(100); }
                IOMsg.Instance.CFBol = false;
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.YGZ1_start);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                while (!WuLiaoCoffeeSel.Instance.YGZDW_Bol) { Thread.Sleep(10); }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.YGZ1_end);
                Thread.Sleep(1000);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.YGS1_start);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                while (!WuLiaoCoffeeSel.Instance.YGSDW_Bol) { Thread.Sleep(10); }
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.YGS1_end);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                IOMsg.Instance.CFBol = true;
                Thread.Sleep(200);
            }
            catch (Exception)
            {
            }
        }
        public void MakeMealMsg(string id, int ccInit, int ordergoodsid, int orderid)
        {
            try
            {

                ClientList clientList1 = new ClientList();
                clientList1.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList1.code = 12;
                if (ccInit == 1)
                {
                    ClientDP.Instance.IsMakeInit1 = 2;
                    if (ClientDP.Instance.OrderId2 == orderid)
                    {
                        ClientDP.Instance.OrderId1 = orderid;
                        ClientDP.Instance.orderNum1 = id;
                        ClientDP.Instance.OrderId2 = 0;
                        ClientDP.Instance.IsMakeInit2 = 0;
                        ClientDP.Instance.orderNum2 = "";
                    }
                }
                else
                    ClientDP.Instance.IsMakeInit2 = 2;
                clientList1.DP = ClientDP.Instance;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList1)));
                while (!IOMsg.Instance.CFBol) { Thread.Sleep(100); }
                IOMsg.Instance.CFBol = false;
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_UP_end);
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_Down_end);
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_Down_start);
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.FYFJ1_start);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.FYFJ2_start);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                while (true)
                {
                    if (ccInit == 1)
                    {
                        if (WuLiaoCoffeeSel.Instance.PushRodDown1_Sel_Bol)
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    else if (ccInit == 2)
                    {
                        if (WuLiaoCoffeeSel.Instance.PushRodDown2_Sel_Bol)
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    Thread.Sleep(10);
                }
                IOMsg.Instance.AllBol = false;
                string ids = "";
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_Down_end);
                if (id.Length > 6)
                    id = id.Substring(id.Length - 6, 6);
                if (id.Length == 6)
                {
                    string read = id.Substring(0, 1) + " " + id.Substring(1, 1) + " " + id.Substring(2, 1) + " " + id.Substring(3, 1) + " " + id.Substring(4, 1) + " " + id.Substring(5, 1) + " ";
                    string read1 = EnglishRead(id.Substring(0, 1)) + " " + EnglishRead(id.Substring(1, 1)) + " " + EnglishRead(id.Substring(2, 1)) + " " + EnglishRead(id.Substring(3, 1)) + " " + EnglishRead(id.Substring(4, 1)) + " " + EnglishRead(id.Substring(5, 1)) + " ";
                    id = read;
                    ids = read1;
                }
                int callidex = 0;
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                IOMsg.Instance.CFBol = true;
                //ClientList clientList = new ClientList();
                //clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                //clientList.code = 10;
                //clientList.IsMake = true;
                //SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                Thread.Sleep(200);
                int ccint = 0;
                while (true)
                {
                    if (ccInit == 1)
                    {
                        if (!WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol)
                        {
                            if (ccint >= 10)
                                break;
                            else
                            {
                                ccint++;
                                Thread.Sleep(100);
                            }
                        }
                        else
                        {
                            if (callidex % 100 == 0)
                                SpeechVoice.Instance.VoicePlayback(" NO." + ids + ",please pick up your coffee ");
                            Thread.Sleep(100);
                            callidex++;
                        }
                    }
                    else
                    {
                        if (!WuLiaoCoffeeSel.Instance.OutMealCup2_Sel_Bol)
                        {
                            if (ccint >= 10)
                                break;
                            else
                            {
                                ccint++;
                                Thread.Sleep(100);
                            }
                        }
                        else
                        {
                            if (callidex % 100 == 0)
                                SpeechVoice.Instance.VoicePlayback(" NO." + ids + ",please pick up your coffee ");
                            Thread.Sleep(100);
                            callidex++;
                        }
                    }
                    Thread.Sleep(100);
                }
                Thread.Sleep(5000);
                SpeechVoice.Instance.VoicePlayback("The door is closing，please watch your hands");
                ClientList clientList2 = new ClientList();
                clientList2.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList2.code = 12;
                if (ccInit == 1)
                {
                    ClientDP.Instance.IsMakeInit1 = 0;
                    ClientDP.Instance.orderNum1 = "";
                    ClientDP.Instance.OrderId1 = 0;
                }
                else
                {
                    ClientDP.Instance.IsMakeInit2 = 0;
                    ClientDP.Instance.orderNum2 = "";
                    ClientDP.Instance.OrderId2 = 0;
                }
                clientList2.DP = ClientDP.Instance;
                SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList2)));

                Thread.Sleep(5000);
                while (true)
                {
                    if (ccInit == 1)
                    {
                        if (!WuLiaoCoffeeSel.Instance.BANNER1_Sel_Bol)
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    else if (ccInit == 2)
                    {
                        if (!WuLiaoCoffeeSel.Instance.BANNER2_Sel_Bol)
                        {
                            break;
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    Thread.Sleep(10);
                }
                Console.WriteLine("yes1");
                OutMealClose(ccInit);
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.FYFJ1_end);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.FYFJ2_end);
                Thread.Sleep(200);
                Console.WriteLine("yes11");
                IOMsg.Instance.AllBol = true;
                IOMsg.Instance.CFBol = true;
                while (true)
                {
                    EditDeviceOrderStatus status = new EditDeviceOrderStatus();
                    status.orderGoodsId = ordergoodsid;
                    status.orderId = orderid;
                    status.status = 5;
                    string lists = JsonConvert.SerializeObject(status);
                    var ddd1 = HttpMsg.Instance.editDeviceOrderStatusMsg(lists);
                    if (ddd1 != null)
                    {
                        if (ddd1 != "")
                        {
                            var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                            if (list10 != null)
                            {
                                if (list10.code == "0" || list10.code == "1")
                                {
                                    Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                    break;
                                }
                            }
                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                        }

                    }
                    Thread.Sleep(100);
                }
                MainWindow.coffeeMaking.Remove(MainWindow.coffeeMaking.FirstOrDefault(a => a.OrderId == orderid && a.OrderGoodsId == ordergoodsid && a.OrderNum == id));
            }
            catch (Exception)
            {
                return;
            }
        }

        /// <summary>
        /// 门升
        /// </summary>
        public void OutMealClose(int ccInit)
        {
            try
            {
                while (!IOMsg.Instance.CFBol) { Thread.Sleep(100); }
                IOMsg.Instance.CFBol = false;
                bool bol = false;
                Console.WriteLine("yes2");
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_UP_end);
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_Down_end);
                Thread.Sleep(200);
                if (ccInit == 1)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
                else if (ccInit == 2)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_UP_start);
                Console.WriteLine("yes3");
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                Thread.Sleep(200);
                if (ccInit == 1)
                {
                    while (true)
                    {
                        Console.WriteLine("yes4");
                        if (!WuLiaoCoffeeSel.Instance.BANNER1_Sel_Bol)
                        {
                            IOMsg.Instance.AllBol = true;
                            if (WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol)
                            {
                                Console.WriteLine("yes7");
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                                Thread.Sleep(200);
                                IOMsg.Instance.AllBol = true;
                                bol = false;
                                break;
                            }
                            Thread.Sleep(200);
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString());
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            while (true)
                            {
                                if (WuLiaoCoffeeSel.Instance.PushRodUP1_Sel_Bol)
                                {
                                    Console.WriteLine("yes9");
                                    IOMsg.Instance.AllBol = false;
                                    Thread.Sleep(200);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                                    Thread.Sleep(200);
                                    IOMsg.Instance.AllBol = true;
                                    bol = false;
                                    break;
                                }
                                if (!WuLiaoCoffeeSel.Instance.BANNER1_Sel_Bol)
                                {
                                    bol = true;
                                    break;
                                }
                                Thread.Sleep(200);
                                Console.WriteLine(DateTime.Now.ToString() + "光栅亮");
                            }
                            if (bol)
                            {
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
                                Console.WriteLine("yes3");
                                Thread.Sleep(200);
                                IOMsg.Instance.AllBol = true;
                                Thread.Sleep(200);
                            }
                        }
                        Thread.Sleep(10);
                    }
                }
                else if (ccInit == 2)
                {
                    while (true)
                    {
                        Console.WriteLine("yes4");
                        if (!WuLiaoCoffeeSel.Instance.BANNER2_Sel_Bol)
                        {
                            IOMsg.Instance.AllBol = true;
                            if (WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol)
                            {
                                Console.WriteLine("yes7");
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_UP_end);
                                Thread.Sleep(200);
                                IOMsg.Instance.AllBol = true;
                                bol = false;
                                break;
                            }
                            Thread.Sleep(200);
                        }
                        else
                        {
                            Console.WriteLine(DateTime.Now.ToString());
                            IOMsg.Instance.AllBol = false;
                            Thread.Sleep(200);
                            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_UP_end);
                            Thread.Sleep(200);
                            IOMsg.Instance.AllBol = true;
                            while (true)
                            {
                                if (WuLiaoCoffeeSel.Instance.PushRodUP2_Sel_Bol)
                                {
                                    Console.WriteLine("yes9");
                                    IOMsg.Instance.AllBol = false;
                                    Thread.Sleep(200);
                                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_UP_end);
                                    Thread.Sleep(200);
                                    IOMsg.Instance.AllBol = true;
                                    bol = false;
                                    break;
                                }
                                if (!WuLiaoCoffeeSel.Instance.BANNER2_Sel_Bol)
                                {
                                    bol = true;
                                    break;
                                }
                                Thread.Sleep(200);
                                Console.WriteLine(DateTime.Now.ToString() + "光栅亮");
                            }
                            if (bol)
                            {
                                IOMsg.Instance.AllBol = false;
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_UP_end);
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_Down_end);
                                Thread.Sleep(200);
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod2_UP_start);
                                Console.WriteLine("yes3");
                                Thread.Sleep(200);
                                IOMsg.Instance.AllBol = true;
                                Thread.Sleep(200);
                            }
                        }
                        Thread.Sleep(10);
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("yesno");
                return;
            }
        }
        public string EnglishRead(string id)
        {
            try
            {
                string name = "";
                switch (id)
                {
                    case "0":
                        name = "Zero";
                        break;
                    case "1":
                        name = "One";
                        break;
                    case "2":
                        name = "Two";
                        break;
                    case "3":
                        name = "Three";
                        break;
                    case "4":
                        name = "Four";
                        break;
                    case "5":
                        name = "Five";
                        break;
                    case "6":
                        name = "Six";
                        break;
                    case "7":
                        name = "Seven";
                        break;
                    case "8":
                        name = "Eight";
                        break;
                    case "9":
                        name = "Nine";
                        break;
                }
                return name;
            }
            catch (Exception)
            {
                return "";
            }
        }
        public delegate void TestDelegateInvoke(string orderNum, int ccInit, int ordergoodsid, int orderid);
        /// <summary>
        /// 取杯
        /// </summary>
        /// <param name="cupCode"></param>
        public void QBMsg(int cupcode)
        {
            try
            {
                Thread.Sleep(200);
                if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                    RobotModelLeft.Instance.RobotQJB1_UP();
                else
                    RobotModelLeft.Instance.RobotQJB2_UP();
                Thread.Sleep(200);
                while (true)
                {
                    Console.WriteLine("daojiebeidian1 " + DateTime.Now.ToString());
                    bool RobotBol = false;
                    Thread.Sleep(200);
                    RobotBol = RobotModelLeft.Instance.LRobotDLBQ_Sel();
                    Thread.Sleep(200);
                    if (RobotBol)
                    {
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotKFJL_Down();
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotQJB1_Down();
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotQJB2_Down();
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotWBTZ_Down();
                        Thread.Sleep(200);
                        break;
                    }
                }
                while (!IOMsg.Instance.CFBol)
                {
                    Thread.Sleep(100);
                }
                IOMsg.Instance.CFBol = false;
                IOMsg.Instance.AllBol = false;
                bool wuliaoBol = WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol;
                Thread.Sleep(200);
                if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_start);
                else
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_start);
                Thread.Sleep(100);
                IOMsg.Instance.AllBol = true;
                Thread.Sleep(100);
                if (wuliaoBol)
                {
                    while (WuLiaoCoffeeSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                    while (!WuLiaoCoffeeSel.Instance.LBQFK1_Bol) { Thread.Sleep(10); }
                }
                else
                {
                    while (WuLiaoCoffeeSel.Instance.LBQFK2_Bol) { Thread.Sleep(10); }
                    while (!WuLiaoCoffeeSel.Instance.LBQFK2_Bol) { Thread.Sleep(10); }
                }
                Thread.Sleep(100);
                IOMsg.Instance.AllBol = false;
                Thread.Sleep(100);
                if (wuliaoBol)
                {
                    Thread.Sleep(50);
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup_end);
                }
                else
                    IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Drop_Cup2_end);
                Thread.Sleep(200);
                IOMsg.Instance.AllBol = true;
                IOMsg.Instance.CFBol = true;
                RobotModelLeft.Instance.RobotQBJC_UP();
                Thread.Sleep(100);
                while (true)
                {
                    bool RobotBol = false;
                    RobotBol = RobotModelLeft.Instance.LRobotDJBJCD_Sel();
                    Thread.Sleep(200);
                    if (RobotBol)
                    {
                        Thread.Sleep(200);
                        RobotModelLeft.Instance.RobotQBJC_Down();
                        Thread.Sleep(200);
                        break;
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
